import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

// 在vue-router在3.1.0版本之后，push和replace方法会返回一个promise对象，如果跳转到相同的路由，就报promise uncaught异常.
// 1-降版本
// 2-使用catch捕获异常.
const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function (location) {
	return routerPush.call(this, location).catch((error) => error)
}

const routes = [
	{
		path: '/',
		name: 'home',
		component: () => import('@/view/home.vue'),
	},
	{
		path: '/login',
		name: 'login',
		component: () => import('@/view/login.vue'),
	},
	{
		path: '*',
		name: '404',
		component: () => import('@/view/404/404.vue'),
	},
]

const router = new VueRouter({
	routes,
})

// 路由前置守卫
router.beforeEach((to, from, next) => {
	// do sth 可在这里拦截
	next()
})

export default router
